package com.hgyc.mom.core.handler;

import cn.dev33.satoken.exception.NotLoginException;
import com.hgyc.mom.common.enums.CodeMsg;
import com.hgyc.mom.common.exception.*;
import com.hgyc.mom.common.exception.job.TaskException;
import com.hgyc.mom.common.vo.Result;
import jakarta.validation.ValidationException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 * @author fengwj
 * @since  2019-12-13 10:07
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    // 处理 Sa-Token 未登录异常
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ExceptionHandler(NotLoginException.class)
    public String handleNotLoginException(NotLoginException e) {
        // 返回自定义错误信息
        return "未登录或登录超时，请重新登录";
    }

    /**
     * 处理自定义异常
     */
    @ExceptionHandler(BizException.class)
    public Result<?> handleRRException(BizException e) {
        log.error(e.getMessage(), e);
        return Result.error(e.getMessage());
    }

    /**
     * 方法参数校验
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result<?> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        log.error(e.getMessage(), e);
        return Result.error(CodeMsg.PARAM_FAIL);
    }

    /**
     * ValidationException
     */
    @ExceptionHandler(ValidationException.class)
    public Result<?> handleValidationException(ValidationException e) {
        log.error(e.getMessage(), e);
        return Result.error(CodeMsg.VALIDATION_FAIL);
    }

    /**
     * 创建数据已存在异常
     * @param e
     * @return
     */
    @ExceptionHandler(EntityExistException.class)
    public Result<?> handleEntityExistException(EntityExistException e) {
        log.error(e.getMessage(), e);
        return Result.error(CodeMsg.ENTITY_EXIST);
    }

    /**
     * 创建数据异常
     * @param e
     * @return
     */
    @ExceptionHandler(SaveException.class)
    public Result<?> handleEntityExistException(SaveException e) {
        log.error(e.getMessage(), e);
        return Result.error(CodeMsg.SAVE_FAIL);
    }


    @ExceptionHandler(DuplicateKeyException.class)
    public Result<?> handleDuplicateKeyException(DuplicateKeyException e) {
        log.error(e.getMessage(), e);
        return Result.error(CodeMsg.ENTITY_EXIST);
    }

    @ExceptionHandler(HttpMessageNotReadableException.class)
    public Result<?> handleJsonException(HttpMessageNotReadableException e) {
        log.error(e.getMessage(), e);
        return Result.error(CodeMsg.PARAM_FAIL);
    }

    @ExceptionHandler(FlowException.class)
    public Result<?> handleFlowException(FlowException e) {
        log.error(e.getMessage(), e);
        return Result.error(e.getMessage());
    }

    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public Result<?> handleRequestMethodException(HttpRequestMethodNotSupportedException e) {
        log.error(e.getMessage(), e);
        return Result.error(CodeMsg.REQUEST_METHOD_ERROR);
    }

    @ExceptionHandler(TaskException.class)
    public Result<?> handleJobException(TaskException e) {
        log.error(e.getMessage(), e);
        return Result.error(CodeMsg.JOB_ERROR);
    }

    @ExceptionHandler(Exception.class)
    public Result<?> handleException(Exception e) {
        log.error(e.getMessage(), e);
        return Result.error("系统繁忙,请稍后再试");
    }
}
